---
title: "Fascist Legacies of Mobilization and Co-Optation: Evidence from Democratic Portugal"
subtitle: "Replication File, Comparative Political Studies Dataverse"
author: 
  - Michael Abertus, University of Chicago
  - Noah Schouela, University of Chicago
date: August 2023
output: pdf_document
---

# Load Packages
```{r, warning = F, message = F}
library(tidyverse) # 1.3.2
library(fastDummies) # 1.6.3
library(sf) # 1.0-12
library(tmap) # 3.3-3

library(stargazer) # 5.2.3
library(systemfit) # 1.1-28
library(estimatr) # 1.0.0
library(fixest) # 0.11.1
```

# Check Working Directory
```{r, warning = F, message = F}
getwd()
```

# Plotting Functions
```{r, warning = F, message = F}
source("Plotting Functions.R")
```

# Read in shapefiles
```{r, warning = F, message = F}
load("shp_portugal.Rdata")
load("shp_parishes.Rdata")
load("shp_halves.Rdata")
load("shp_cities.Rdata")
```

# Read in Data
```{r, warning = F, message = F}

# Read in Variables
load("data_vars.Rdata")

# Variable Descriptions
data.frame(names = attr(data_vars, "names"), labels = attr(data_vars, "labels"))

# Generate Dictionary for Results
dictionary = c(municipal_code = "Municipal",
              smes_pc = "SMEs/Capita",
              sme_intense = "SME Intensity",
              `sme_intense0` = "No Mobilization",
              `sme_intense1-4` = "Sporadic Mobilization",
              `sme_intense5+` = "Endemic Mobilization",
              sme_intense_alt = "SME Intensity (Alt.)", 
              `sme_intense_alt0` = "No Mobilization",
              `sme_intense_alt1-2` = "Sporadic Mobilization (Alt.)",
              `sme_intense_alt3+` = "Endemic Mobilization (Alt.)",
              early_cdp = "Early Casa do Povo",
              `as.factor(early_cdp)0` = "No or Later Casa do Povo",
              `as.factor(early_cdp)1` = "Early Casa do Povo",
              share_cult_rain = "Share of Cultivated Rain-Fed Land", 
              parish_area = "Parish Area",
              dist_lisbon = "Distance to Lisbon",
              repub_conf = "Republican SME",
              repub_ag_synd = "Republican Agricultural Syndicate",
              pcp_office = "PCP Office",
              pr_ha_exporpriated = "Percent of Hectares Expropriated",
              gnr_events_log = "Log(National Guard Deployments)",
              coop_loss = "Change in Number of Cooperatives")

# Read in Ideological Group Vote Share
load("data_ideology.Rdata")

# Read in Turnout Data
load("data_turnout.Rdata")

```

# Summary 
```{r, warning = F, message = F}

# Variables
data_vars %>% filter(country_half == "South", cities == 0) %>% 
  dummy_cols(., select_columns = "sme_intense") %>% 
  select(smes_pc, contains("sme_intense_"), early_cdp, share_cult_rain, parish_area, dist_lisbon, repub_conf, repub_ag_synd, pr_ha_exporpriated, gnr_events_log, coop_loss, pcp_office) %>% select(-sme_intense_alt) %>% 
  data.frame() %>%
  stargazer(., median = T, type = "text", 
            covariate.labels = c("SMEs/Capita (*1000), 1947-1962", "No Mobilization (0 SMEs)", "Sporadic Mobilization (1-4 SMEs)", "Endemic Mobilization (5+ SMEs)", "Early Casa do Povo, by 1947", "Share of Cultivated Rain-Fed Land", "Parish Area (Km2)", "Distance to Lisbon (Km)", "Republican Mobilization, 1910-1914", "Republican Agricultural Syndicate, by Aug. 1922", "% of Ha. Expropriated, 1975-77", "Log(National Guard Deployments), 1976-1983", "Change in # of Cooperatives, 1976-1986", "PCP Office, 1974-1975"))

# Vote Share
data_vars %>% filter(country_half == "South", cities == 0) %>% 
  left_join(do.call(rbind, data_ideology)) %>% 
  select(parish_code, Communist, Center, Right) %>% mutate(across(c(Communist, Center, Right), ~.x*100)) %>% 
  select(-parish_code) %>% data.frame() %>% 
  stargazer(., median = T, digits = 2, type = "text")

# Turnout 
data_vars %>% filter(country_half == "South", cities == 0) %>% 
  left_join(do.call(rbind, data_turnout))  %>% select(parish_code, year, contains("Reg"), contains("Vot")) %>% 
  pivot_longer(!c(parish_code, year), names_to = "var", values_to = "voters") %>% filter(!is.na(voters)) %>% 
  mutate(var = ifelse(str_detect(var, "Reg"), "Registers", "Voters")) %>% 
  pivot_wider(c(parish_code, year), names_from = var, values_from = voters) %>% mutate(Turnout = Voters/Registers*100) %>% 
  select(Turnout) %>% data.frame() %>% 
  stargazer(., median = T, digits = 2, type = "text")

```

# Figure 1: SMEs
```{r, warning = F, message = F}

data_vars %>%
  filter(country_half == "South", cities == 0) %>%
  filter(!is.na(smes)) %>%
  left_join(shp_parishes) %>% st_as_sf() %>%
  tm_shape() +
  tm_polygons("smes", breaks = c(0, 0.001, 2, 3, 5, 10, 15, 40),
           labels = c("0", "1-2","2-3", "3-5", "5-10", "10-15", "15-40"),
           palette = "Greys", title = "Social \nMobilization \nEvents") + tm_legend(legend.outside=T)

data_vars %>%
  filter(!is.na(smes), cities == 0) %>%
  left_join(shp_parishes) %>% st_as_sf() %>%
  tm_shape() +
  tm_fill("smes", breaks = c(0, 0.001, 2, 3, 5, 10, 15, 40),
           labels = c("0", "1-2","2-3", "3-5", "5-10", "10-15", "15-40"),
           palette = "Greys", title = "Social \nMobilization \nEvents") + tm_legend(legend.outside=T) +
  tm_shape(shp_halves) + tm_borders(lwd = 2) +
  tm_shape(shp_cities) + tm_text("District", size = 0.5)

```

# Figure 2: CdP
```{r, warning = F, message = F}

tm_shape(shp_parishes %>% left_join(data_vars) %>% filter(country_half == "South", cities == 0)) +
  tm_polygons("early_cdp", style = "cat",
           palette = c("grey90", "grey10"), title = "Early Casas\n do Povo", colorNA = "white") + tm_legend(legend.outside=T)

data_vars %>%
  filter(country_half == "South") %>%
  ggplot(aes(x=cdp_year_partial)) + geom_histogram() + theme_light() +
  xlab("Year of Casa do Povo Establishment") + ylab("Number of Parishes") + labs(caption = "Southern Portugal")

```

# Ideology Models
```{r, warning = F, message = F}

ideos <- c( "Communist", "Center", "Right")
ideology <- list()
for (j in 1:length(ideos)){ # j <- 1
  years <- list()
      for (i in 1:length(data_ideology)){ # i <- 1
        models <- list() 
        data_temp <- data_vars %>% left_join(data_ideology[[i]])
# Interaction, SME/Capita       
        models[[1]] <- feols(as.formula(paste(ideos[j], "smes_pc * early_cdp + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd | municipal_code", sep = "~")), data = data_temp %>% filter(country_half == "South", cities == 0))
# Interaction, SME Intensity
        models[[2]] <- feols(as.formula(paste(ideos[j], "sme_intense * as.factor(early_cdp) + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd | municipal_code", sep = "~")), data = data_temp %>% filter(country_half == "South", cities == 0))
# Marginal, SME/Capita    
        models[[3]] <- feols(as.formula(paste(ideos[j], "as.factor(early_cdp) / smes_pc  + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd| municipal_code", sep = "~")), data = data_temp %>% filter(country_half == "South", cities == 0))
# Marginal, SME Intensity        
        models[[4]] <- feols(as.formula(paste(ideos[j], "as.factor(early_cdp) / sme_intense  + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd | municipal_code", sep = "~")), data = data_temp %>% filter(country_half == "South", cities == 0))
# Marginal, SME Intensity, Alternative Coding       
        models[[5]] <- feols(as.formula(paste(ideos[j], "as.factor(early_cdp) / sme_intense_alt  + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd | municipal_code", sep = "~")), data = data_temp %>% filter(country_half == "South", cities == 0))
        years[[i]] <- models
      }
  ideology[[j]] <- years
}

```

## Communist Tabular
```{r, warning = F, message = F}
# # Interaction, SME/Capita
# etable(lapply(ideology[[1]], `[[`, 1), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
       # headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011"), dict = dictionary, 
       # order = c("Early Casa do Povo", "No or Later Casa do Povo"))
# # Interaction, SME Intensity
# etable(lapply(ideology[[1]], `[[`, 2), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
#        headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011"), dict = dictionary,
#        order = c("Early Casa do Povo", "No or Later Casa do Povo")))
# Marginal, SME/Capita
etable(lapply(ideology[[1]], `[[`, 3), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
       headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011"), dict = dictionary, 
       order = c("Early Casa do Povo", "No or Later Casa do Povo"))
# Marginal, SME Intensity
etable(lapply(ideology[[1]], `[[`, 4), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
       headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011"), dict = dictionary, 
       order = c("Early Casa do Povo", "No or Later Casa do Povo"))
```

## Center Tabular
```{r, warning = F, message = F}
# # Interaction, SME/Capita
# etable(lapply(ideology[[2]], `[[`, 1), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
       # headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011"), dict = dictionary, 
       # order = c("Early Casa do Povo", "No or Later Casa do Povo"))
# # Interaction, SME Intensity
# etable(lapply(ideology[[2]], `[[`, 2), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
#        headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011"), dict = dictionary,
#        order = c("Early Casa do Povo", "No or Later Casa do Povo")))
# Marginal, SME/Capita
etable(lapply(ideology[[2]], `[[`, 3), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
       headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011"), dict = dictionary, 
       order = c("Early Casa do Povo", "No or Later Casa do Povo"))
# Marginal, SME Intensity
etable(lapply(ideology[[2]], `[[`, 4), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
       headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011"), dict = dictionary, 
       order = c("Early Casa do Povo", "No or Later Casa do Povo"))
```

## Right Tabular
```{r, warning = F, message = F}
# # Interaction, SME/Capita
# etable(lapply(ideology[[3]], `[[`, 1), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
       # headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011"), dict = dictionary, 
       # order = c("Early Casa do Povo", "No or Later Casa do Povo"))
# # Interaction, SME Intensity
# etable(lapply(ideology[[3]], `[[`, 2), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
#        headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011"), dict = dictionary,
#        order = c("Early Casa do Povo", "No or Later Casa do Povo")))
# Marginal, SME/Capita
etable(lapply(ideology[[3]], `[[`, 3), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
       headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011"), dict = dictionary, 
       order = c("Early Casa do Povo", "No or Later Casa do Povo"))
# Marginal, SME Intensity
etable(lapply(ideology[[3]], `[[`, 4), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
       headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011"), dict = dictionary, 
       order = c("Early Casa do Povo", "No or Later Casa do Povo"))
```

## Plot Results
```{r, warning = F, message = F}

# # Interaction, SME/Capita
# coef_plots_multivar_allideos_long(1,  c("smes_pc", "smes_pc x early_cdp")) + labs(caption = "Conflicts/Capita, Interactive Effects")+ 
#   scale_color_discrete(name = "Interactive Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP")) + 
#   theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ 
#   theme(legend.position="bottom") + ggtitle("")
# 
# # Interaction, SME Intensity
# etable(lapply(ideology[[1]], `[[`, 2), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
#        headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011")) %>%
#   coef_plots_facetvar(., c("sme_intense1-4", "sme_intense5+",
#                            "sme_intense1-4 x as.factor(early_cdp)1", "sme_intense5+ x as.factor(early_cdp)1"))  +
#   scale_color_discrete(name = "Interactive Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP"))+
#   theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
#   theme(legend.position="bottom") + ggtitle("")

# Marginal, SME/Capita (Figure 3)
coef_plots_multivar_allideos_long(3,  c("as.factor(early_cdp)0 x smes_pc", "as.factor(early_cdp)1 x smes_pc")) + 
  scale_color_discrete(name = "Marginal Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP")) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ 
  theme(legend.position="bottom") + ggtitle("")

# # Marginal, SME Intensity
# etable(lapply(ideology[[1]], `[[`, 4), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
#        headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011")) %>%
#   coef_plots_facetvar(., c("as.factor(early_cdp)0 x sme_intense1-4", "as.factor(early_cdp)1 x sme_intense1-4", 
#                   "as.factor(early_cdp)0 x sme_intense5+", "as.factor(early_cdp)1 x sme_intense5+"))  + 
#   scale_color_discrete(name = "Marginal Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP"))+ 
#   theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ 
#   theme(legend.position="bottom")+ ggtitle("")

# Marginal, SME Intensity. Just Endemic (Figure 4)
etable(lapply(ideology[[1]], `[[`, 4), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
       headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011")) %>%
  coef_plots_facetvar_justpcp(., c("as.factor(early_cdp)0 x sme_intense1-4", "as.factor(early_cdp)1 x sme_intense1-4", 
                  "as.factor(early_cdp)0 x sme_intense5+", "as.factor(early_cdp)1 x sme_intense5+"))  + 
  scale_color_discrete(name = "Marginal Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP"))+ 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ 
  theme(legend.position="bottom")+ ggtitle("")

```

## Alternative Coding of Conflict Intensity
```{r, warning = F, message = F}

# Marginal Effect, Tabular
etable(lapply(ideology[[1]], `[[`, 5), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
       headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011"), dict = dictionary, 
       order = c("Early Casa do Povo", "No or Later Casa do Povo"))

# Marginal Effect, Plot
etable(lapply(ideology[[1]], `[[`, 5), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
       headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011")) %>%
  coef_plots_facetvar_alt(., c("as.factor(early_cdp)0 x sme_intense_alt1-2", "as.factor(early_cdp)1 x sme_intense_alt1-2",
                  "as.factor(early_cdp)0 x sme_intense_alt3+", "as.factor(early_cdp)1 x sme_intense_alt3+"))  +
  scale_color_discrete(name = "Marginal Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP"))+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + 
  labs(caption = "Sporadic Mobilization = 1-2 SMEs; Endemic Mobilization = 3+ SMEs")+ 
  theme(legend.position="bottom")+ ggtitle("")

```

# Turnout
```{r, warning = F, message = F}
years <- list()
    for (i in 1:length(data_turnout)){ # i <- 1
      models <- list()
elec_temp <- data_turnout[[i]]
colnames(elec_temp) <- sapply(str_split(colnames(elec_temp),"_") , "[[", 1)
colnames(elec_temp)[2] <- "parish_code" 
data_temp <- data_vars %>% left_join(elec_temp %>% mutate(TO = Vote/Reg))
# Interaction, SME/Capita
        models[[1]] <- feols(as.formula(paste("TO", "smes_pc * early_cdp + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd  | municipal_code", sep = "~")), data = data_temp %>% filter(country_half == "South", cities == 0))
# Interaction, SME Intensity        
        models[[2]] <- feols(as.formula(paste("TO", "sme_intense * as.factor(early_cdp) + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd  | municipal_code", sep = "~")), data = data_temp %>% filter(country_half == "South", cities == 0))
# Marginal, SME/Capita              
        models[[3]] <- feols(as.formula(paste("TO", "as.factor(early_cdp) / smes_pc + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd  | municipal_code", sep = "~")), data = data_temp %>% filter(country_half == "South", cities == 0))
# Marginal, SME Intensity        
        models[[4]] <- feols(as.formula(paste("TO", "as.factor(early_cdp) / sme_intense + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd   | municipal_code", sep = "~")), data = data_temp %>% filter(country_half == "South", cities == 0))
                           
    years[[i]] <- models
  }
```

## Turnout Tabular
```{r, warning = F, message = F}
# # Interaction, SME/Capita
# etable(lapply(years, `[[`, 1), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
#        headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011"), dict = dictionary, 
#        order = c("Early Casa do Povo", "No or Later Casa do Povo"))
# # Interaction, SME Intensity
# etable(lapply(years, `[[`, 2), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
#        headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011"), dict = dictionary, 
#        order = c("Early Casa do Povo", "No or Later Casa do Povo"))
# Marginal, SME/Capita
etable(lapply(years, `[[`, 3), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
       headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011"), dict = dictionary, 
       order = c("Early Casa do Povo", "No or Later Casa do Povo"))
# Marginal, SME Intensity
etable(lapply(years, `[[`, 4), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
       headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011"), dict = dictionary, 
       order = c("Early Casa do Povo", "No or Later Casa do Povo"))
```

## Plot Results
```{r, warning = F, message = F}
# # Interaction, SME/Capita
# etable(lapply(years, `[[`, 1), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
#        headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011")) %>%
#   coef_plots_multivar(., c("smes_pc", "smes_pc x early_cdp"))   + 
#   scale_color_discrete(name = "Interactive Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP")) + 
#   theme(legend.position="bottom") + ggtitle("")

# # Interaction, SME Intensity
# etable(lapply(years, `[[`, 2), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
#        headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011")) %>%
#   coef_plots_facetvar(., c("sme_intense1-4", "sme_intense5+", 
#                            "sme_intense1-4 x as.factor(early_cdp)1", "sme_intense5+ x as.factor(early_cdp)1"))  + 
#   scale_color_discrete(name = "Interactive Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP"))+ 
#   theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ 
#   theme(legend.position="bottom") + ggtitle("")

# Marginal, SME/Capita (Figure 5)
etable(lapply(years, `[[`, 3), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
       headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011")) %>%
  coef_plots_multivar(., c("as.factor(early_cdp)0 x smes_pc", "as.factor(early_cdp)1 x smes_pc"))  + 
  scale_color_discrete(name = "Marginal Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP"))+ 
  theme(legend.position="bottom") + ggtitle("")

# # Marginal, SME Intensity
# etable(lapply(years, `[[`, 4), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
#        headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011")) %>%
#   coef_plots_facetvar(., c("as.factor(early_cdp)0 x sme_intense1-4", "as.factor(early_cdp)1 x sme_intense1-4", 
#                   "as.factor(early_cdp)0 x sme_intense5+", "as.factor(early_cdp)1 x sme_intense5+"))  + 
#   scale_color_discrete(name = "Marginal Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP"))+ 
#   theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ 
#   theme(legend.position="bottom") + ggtitle("")
```

# PCP Offices (Table 1)
```{r, warning = F, message = F}
models <- list()

models[[1]] <- feols(pcp_office ~ early_cdp + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd | municipal_code, 
                     data = data_vars %>% filter(country_half == "South", cities == 0))

etable(models[1], vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F, dict = dictionary)
```

# Republican (Table 2)
```{r, warning = F, message = F}

models <- list()

models[[1]] <- feols(early_cdp ~ repub_conf + parish_area + dist_lisbon + share_cult_rain | municipal_code, data = data_vars %>% filter(country_half == "South", cities == 0))
models[[2]] <- feols(early_cdp ~ repub_ag_synd + parish_area + dist_lisbon + share_cult_rain | municipal_code, data = data_vars %>% filter(country_half == "South", cities == 0))
models[[3]] <- feols(early_cdp ~ repub_conf + repub_ag_synd + parish_area + dist_lisbon + share_cult_rain | municipal_code, data = data_vars %>% filter(country_half == "South", cities == 0))

models[[4]] <- feols(smes_pc ~ repub_conf + parish_area + dist_lisbon + share_cult_rain | municipal_code, data = data_vars %>% filter(country_half == "South", cities == 0))
models[[5]] <- feols(smes_pc ~ repub_ag_synd + parish_area + dist_lisbon + share_cult_rain | municipal_code, data = data_vars %>% filter(country_half == "South", cities == 0))
models[[6]] <- feols(smes_pc ~ repub_conf + repub_ag_synd + parish_area + dist_lisbon + share_cult_rain | municipal_code, data = data_vars %>% filter(country_half == "South", cities == 0))

etable(models, vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F, dict = dictionary, 
       order = c("Republican SME", "Republican Agricultural Syndicate"))

```

# Broader Consequences (Table 3)
```{r, warning = F, message = F}

models <- list()

models[[1]] <- feols(gnr_events_log ~ early_cdp + smes_pc +  pr_ha_exporpriated + parish_area + dist_lisbon + share_cult_rain  + repub_conf + repub_ag_synd   | municipal_code,  data = data_vars %>% filter(country_half == "South", cities == 0, pr_ha_exporpriated > 0))
models[[2]] <- feols(coop_loss ~ early_cdp + smes_pc +  pr_ha_exporpriated + parish_area + dist_lisbon + share_cult_rain  + repub_conf + repub_ag_synd   | municipal_code,  data = data_vars %>% filter(country_half == "South", cities == 0, pr_ha_exporpriated > 0))

etable(models, vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F, dict = dictionary)

```

# Mediator Test (Appendix 2.c)
```{r, warning = F, message = F}
models <- list()

models[[1]] <- feols(smes_pc ~ early_cdp  | municipal_code, data = data_vars %>% filter(country_half == "South", cities == 0))
models[[2]] <- feols(smes_pc ~ early_cdp + parish_area + dist_lisbon + share_cult_rain | municipal_code, data = data_vars %>% filter(country_half == "South", cities == 0))
models[[3]] <- feols(smes_pc ~ early_cdp + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd  | municipal_code, data = data_vars %>% filter(country_half == "South", cities == 0))

etable(models, vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F, dict = dictionary)
```









\newpage
## National Results, i.e., Not Restricted to South (Appendix 3.a)

# Ideology Models
```{r, warning = F, message = F}

ideos <- c( "Communist", "Center", "Right")
ideology <- list()
for (j in 1:length(ideos)){ # j <- 1
  years <- list()
      for (i in 1:length(data_ideology)){ # i <- 1
        models <- list() 
        data_temp <- data_vars %>% left_join(data_ideology[[i]])
# Interaction, SME/Capita       
        models[[1]] <- feols(as.formula(paste(ideos[j], "smes_pc * early_cdp + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd | municipal_code", sep = "~")), data = data_temp %>% filter(cities == 0))
# Interaction, SME Intensity
        models[[2]] <- feols(as.formula(paste(ideos[j], "sme_intense * as.factor(early_cdp) + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd | municipal_code", sep = "~")), data = data_temp %>% filter(cities == 0))
# Marginal, SME/Capita    
        models[[3]] <- feols(as.formula(paste(ideos[j], "as.factor(early_cdp) / smes_pc  + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd| municipal_code", sep = "~")), data = data_temp %>% filter(cities == 0))
# Marginal, SME Intensity        
        models[[4]] <- feols(as.formula(paste(ideos[j], "as.factor(early_cdp) / sme_intense  + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd | municipal_code", sep = "~")), data = data_temp %>% filter(cities == 0))
        years[[i]] <- models
      }
  ideology[[j]] <- years
}

```

## Plot Results
```{r, warning = F, message = F}

# # Interaction, SME/Capita
# coef_plots_multivar_allideos_long(1,  c("smes_pc", "smes_pc x early_cdp")) + labs(caption = "Conflicts/Capita, Interactive Effects")+ 
#   scale_color_discrete(name = "Interactive Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP")) + 
#   theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ 
#   theme(legend.position="bottom") + ggtitle("")
# 
# # Interaction, SME Intensity
# etable(lapply(ideology[[1]], `[[`, 2), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
#        headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011")) %>%
#   coef_plots_facetvar(., c("sme_intense1-4", "sme_intense5+",
#                            "sme_intense1-4 x as.factor(early_cdp)1", "sme_intense5+ x as.factor(early_cdp)1"))  +
#   scale_color_discrete(name = "Interactive Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP"))+
#   theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+
#   theme(legend.position="bottom") + ggtitle("")

# Marginal, SME/Capita (Figure 3)
coef_plots_multivar_allideos_long(3,  c("as.factor(early_cdp)0 x smes_pc", "as.factor(early_cdp)1 x smes_pc")) + 
  scale_color_discrete(name = "Marginal Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP")) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ 
  theme(legend.position="bottom") + ggtitle("")

# # Marginal, SME Intensity
# etable(lapply(ideology[[1]], `[[`, 4), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
#        headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011")) %>%
#   coef_plots_facetvar(., c("as.factor(early_cdp)0 x sme_intense1-4", "as.factor(early_cdp)1 x sme_intense1-4", 
#                   "as.factor(early_cdp)0 x sme_intense5+", "as.factor(early_cdp)1 x sme_intense5+"))  + 
#   scale_color_discrete(name = "Marginal Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP"))+ 
#   theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ 
#   theme(legend.position="bottom")+ ggtitle("")

# Marginal, SME Intensity. Just Endemic (Figure 4)
etable(lapply(ideology[[1]], `[[`, 4), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
       headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011")) %>%
  coef_plots_facetvar_justpcp(., c("as.factor(early_cdp)0 x sme_intense1-4", "as.factor(early_cdp)1 x sme_intense1-4", 
                  "as.factor(early_cdp)0 x sme_intense5+", "as.factor(early_cdp)1 x sme_intense5+"))  + 
  scale_color_discrete(name = "Marginal Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP"))+ 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ 
  theme(legend.position="bottom")+ ggtitle("")

```

# Turnout
```{r, warning = F, message = F}
years <- list()
    for (i in 1:length(data_turnout)){ # i <- 1
      models <- list()
elec_temp <- data_turnout[[i]]
colnames(elec_temp) <- sapply(str_split(colnames(elec_temp),"_") , "[[", 1)
colnames(elec_temp)[2] <- "parish_code" 
data_temp <- data_vars %>% left_join(elec_temp %>% mutate(TO = Vote/Reg))
# Interaction, SME/Capita
        models[[1]] <- feols(as.formula(paste("TO", "smes_pc * early_cdp + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd  | municipal_code", sep = "~")), data = data_temp %>% filter(cities == 0))
# Interaction, SME Intensity        
        models[[2]] <- feols(as.formula(paste("TO", "sme_intense * as.factor(early_cdp) + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd  | municipal_code", sep = "~")), data = data_temp %>% filter(cities == 0))
# Marginal, SME/Capita              
        models[[3]] <- feols(as.formula(paste("TO", "as.factor(early_cdp) / smes_pc + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd  | municipal_code", sep = "~")), data = data_temp %>% filter(cities == 0))
# Marginal, SME Intensity        
        models[[4]] <- feols(as.formula(paste("TO", "as.factor(early_cdp) / sme_intense + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd   | municipal_code", sep = "~")), data = data_temp %>% filter(cities == 0))
                           
    years[[i]] <- models
  }
```

## Plot Results
```{r, warning = F, message = F}
# # Interaction, SME/Capita
# etable(lapply(years, `[[`, 1), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
#        headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011")) %>%
#   coef_plots_multivar(., c("smes_pc", "smes_pc x early_cdp"))   + 
#   scale_color_discrete(name = "Interactive Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP")) + 
#   theme(legend.position="bottom") + ggtitle("")

# # Interaction, SME Intensity
# etable(lapply(years, `[[`, 2), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
#        headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011")) %>%
#   coef_plots_facetvar(., c("sme_intense1-4", "sme_intense5+", 
#                            "sme_intense1-4 x as.factor(early_cdp)1", "sme_intense5+ x as.factor(early_cdp)1"))  + 
#   scale_color_discrete(name = "Interactive Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP"))+ 
#   theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ 
#   theme(legend.position="bottom") + ggtitle("")

# Marginal, SME/Capita (Figure 5)
etable(lapply(years, `[[`, 3), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
       headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011")) %>%
  coef_plots_multivar(., c("as.factor(early_cdp)0 x smes_pc", "as.factor(early_cdp)1 x smes_pc"))  + 
  scale_color_discrete(name = "Marginal Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP"))+ 
  theme(legend.position="bottom") + ggtitle("")

# # Marginal, SME Intensity
# etable(lapply(years, `[[`, 4), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
#        headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011")) %>%
#   coef_plots_facetvar(., c("as.factor(early_cdp)0 x sme_intense1-4", "as.factor(early_cdp)1 x sme_intense1-4", 
#                   "as.factor(early_cdp)0 x sme_intense5+", "as.factor(early_cdp)1 x sme_intense5+"))  + 
#   scale_color_discrete(name = "Marginal Effects", labels = c("Social mobilization, later/no CdP", "Social mobilization, early CdP"))+ 
#   theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ 
#   theme(legend.position="bottom") + ggtitle("")
```

# PCP Offices
```{r, warning = F, message = F}
models <- list()

models[[1]] <- feols(pcp_office ~ early_cdp + parish_area + dist_lisbon + share_cult_rain + repub_conf + repub_ag_synd | municipal_code, 
                     data = data_vars %>% filter(cities == 0))

etable(models[1], vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F, dict = dictionary)
```

# Republican
```{r, warning = F, message = F}

models <- list()

models[[1]] <- feols(early_cdp ~ repub_conf + parish_area + dist_lisbon + share_cult_rain | municipal_code, data = data_vars %>% filter(cities == 0))
models[[2]] <- feols(early_cdp ~ repub_ag_synd + parish_area + dist_lisbon + share_cult_rain | municipal_code, data = data_vars %>% filter(cities == 0))
models[[3]] <- feols(early_cdp ~ repub_conf + repub_ag_synd + parish_area + dist_lisbon + share_cult_rain | municipal_code, data = data_vars %>% filter(cities == 0))

models[[4]] <- feols(smes_pc ~ repub_conf + parish_area + dist_lisbon + share_cult_rain | municipal_code, data = data_vars %>% filter(cities == 0))
models[[5]] <- feols(smes_pc ~ repub_ag_synd + parish_area + dist_lisbon + share_cult_rain | municipal_code, data = data_vars %>% filter(cities == 0))
models[[6]] <- feols(smes_pc ~ repub_conf + repub_ag_synd + parish_area + dist_lisbon + share_cult_rain | municipal_code, data = data_vars %>% filter(cities == 0))

etable(models, vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F, dict = dictionary, 
       order = c("Republican SME", "Republican Agricultural Syndicate"))

```

# Broader Consequences
```{r, warning = F, message = F}

models <- list()

models[[1]] <- feols(gnr_events_log ~ early_cdp + smes_pc +  pr_ha_exporpriated + parish_area + dist_lisbon + share_cult_rain  + repub_conf + repub_ag_synd   | municipal_code,  data = data_vars %>% filter(cities == 0, pr_ha_exporpriated > 0))
models[[2]] <- feols(coop_loss ~ early_cdp + smes_pc +  pr_ha_exporpriated + parish_area + dist_lisbon + share_cult_rain  + repub_conf + repub_ag_synd   | municipal_code,  data = data_vars %>% filter(cities == 0, pr_ha_exporpriated > 0))

etable(models, vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F, dict = dictionary)

```

